package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Equipement.Equipment;
import com.neu.cloudfactory.bean.Equipement.Equipmentstatus;
import com.neu.cloudfactory.bean.Equipement.Equipmenttype;
import com.neu.cloudfactory.bean.Factory.Factory;
import com.neu.cloudfactory.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@RestController
public class EquipmentControler {
    @Autowired
    EquipmenttypeRepository equipmenttypeRepository;
    @Autowired
    EquipmentRepository equipmentRepository;
    @Autowired
    EquipmentstatusRepository equipmentstatusRepository;
    @Autowired
    FactoryRepository factoryRepository;
    @Autowired
    LoanRepository loanRepository;
    @Autowired
    CapactityRepository capactityRepository;

    @RequestMapping("/equipment/findAll/{page}/{size}")
    public Page<Equipment> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmentRepository.findAll(pageable);
    }

    @RequestMapping("/equipment/add")
    public String addEquipment(@RequestBody Equipment equipment){
        return equipmentRepository.saveAndFlush(equipment).getId()==null?"failed":"success";
    }

    @RequestMapping("/equipment/deleteById/{id}")
    public String delete(@PathVariable("id") Integer id){
        try{
            Equipment equipment = equipmentRepository.findById(id).get();
            equipmentRepository.delete(equipment);
            capactityRepository.deleteAllByEquipmentid(id);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    @RequestMapping("/equipment/findById/{id}")
    public Equipment findById(@PathVariable("id") Integer id){
        return equipmentRepository.findById(id).get();
    }

    @RequestMapping("/equipment/findAllByFactoryid/{page}/{size}/{factoryid}")
    public Page<Equipment> findAllByFactoryid(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("factoryid") Integer factoryid){
        Pageable pageable = PageRequest.of(page-1,size);
        try{
            return equipmentRepository.findAllByFactoryid(factoryid,pageable);
        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping("/equipment/return/{equipmentid}")
    public String Return(@PathVariable("equipmentid") Integer id){
        Equipment equipment;
        boolean complete = false;
        try{
            equipment = equipmentRepository.getById(id);
            equipment.setFactoryid(0);
            equipment = equipmentRepository.save(equipment);

            loanRepository.deleteAllByEquipmentid(equipment.getId());

            complete=equipment.getFactoryid()==0;
        }catch (Exception e){
            System.out.println(e);
        }
        return complete?"success":"failed";
    }

    @RequestMapping("/equipment/update")
    public String update(@RequestBody Equipment equipment){
        if(equipment.getId()!=null){
            equipment = equipmentRepository.save(equipment);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/equipment/changeStatus/{id}/{statusid}")
    public Equipment changeStaus(@PathVariable("id")Integer id, @PathVariable("statusid") Integer statusid){
        Equipment equipment = equipmentRepository.findById(id).get();
        if(equipment!=null){
            switch (statusid){
                case 1:
                    equipment.setStatusid(3);
                    break;
                case 3:
                    equipment.setStatusid(1);
                    break;
                default:
                    break;
            }
            equipmentRepository.save(equipment);
            return equipment;
        }
        return null;
    }

    @RequestMapping("/equipment/factorysearch/{page}/{size}/{input}/{factoryid}")
    public Page<Equipment> factorysearch(@PathVariable("input") String input,@PathVariable("page") Integer page, @PathVariable("size") Integer size,@PathVariable("factoryid") Integer factoryid){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentRepository.findAllByIdOrNameContainingOrDescriptionContainingOrFactoryid(id,input,input,factoryid,pageable);
    }
    @RequestMapping("/equipment/search/{page}/{size}/{input}")
    public Page<Equipment> search(@PathVariable("input") String input,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentRepository.findAllByIdOrNameContainingOrDescriptionContainingOrFactoryid(id,input,input,id,pageable);
    }

    @RequestMapping("/equipmenttype/add")
    public String addTtype(@RequestBody Equipmenttype equipmenttype){
        return equipmenttypeRepository.saveAndFlush(equipmenttype).getId()==null?"failed":"success";
    }

    @RequestMapping("/equipmenttype/findAll/{page}/{size}")
    public Page<Equipmenttype> typefindAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmenttypeRepository.findAll(pageable);
    }

    @RequestMapping("/equipmenttype/findById/{id}")
    public Equipmenttype findtypeById(@PathVariable("id") Integer id){
        return equipmenttypeRepository.findById(id).get();
    }

    @RequestMapping("/equipmenttype/update")
    public String update(@RequestBody Equipmenttype equipmenttype){
        if(equipmenttype.getId()!=null){
            equipmenttypeRepository.save(equipmenttype);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/equipmenttype/deleteById/{id}")
    public String deletetype(@PathVariable("id") Integer id){
        try{
            Equipmenttype equipmenttype = equipmenttypeRepository.findById(id).get();
            equipmenttypeRepository.delete(equipmenttype);
            List<Equipment> equipments = equipmentRepository.findAllByTypeid(id);
            for (int i=0;i<equipments.size();i++){
                equipments.get(i).setTypeid(1);
            }
            equipmentRepository.saveAll(equipments);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    @RequestMapping("/equipmenttype/search/{page}/{size}/{input}")
    public Page<Equipmenttype> typesearch(@PathVariable("input") String input, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmenttypeRepository.findAllByIdOrTypenameContainingOrDescriptionContaining(id,input,input,pageable);
    }

    @RequestMapping("/equipmentstatus/findAll/{page}/{size}")
    public Page<Equipmentstatus> statusfindAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmentstatusRepository.findAll(pageable);
    }

    @RequestMapping("/equipmentstatus/search/{page}/{size}/{input}")
    public Page<Equipmentstatus> statussearch(@PathVariable("input") String input, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentstatusRepository.findAllByIdOrStatusContaining(id,input,pageable);
    }

    @RequestMapping("/equipmenttype/checkproducttyoe/{typeid}")
    public String checkproducttyoe(@PathVariable("typeid") Integer typeid){
        return equipmentRepository.existsByTypeid(typeid)?"used":"";
    }
}
